using System;
using System.Text;

namespace SG.Productivity.DesktopControlPanel.Support
{
    public static class IParameterCollectionEx
    {
        public static Uri ToUri(this IParameterCollection @this, string mainPart, string scheme = "http", bool usesDoubleSlash = true, string path = "")
        {
            var count = @this.Count;
            var baseUri = new StringBuilder(100 + count * 20);
            baseUri.AppendFormat("{0}:{1}{2}{3}", scheme, usesDoubleSlash ? "//" : "", mainPart,
                                 string.IsNullOrWhiteSpace(path) ? "" : (path[0] == '/' ? path : '/' + path));

            
            for (var i = 0; i < count; i++)
            {
                var sep = i == 0 ? '?' : '&';
                baseUri.Append(sep).Append(@this.GetKey(i)).Append('=').Append(@this.Get(i));
            }

            return new Uri(baseUri.ToString());
        }
    }
}